package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttachmentDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosContractDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstContract;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 组装报文合同信息节点
 * 
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosContractHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String downloadFileFlag = querySceneInstService.getExtParameterValue(context.getSceneInstId(), "downloadFile");
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(context.getSceneInstId());

        if (KeyConsts.IFTRUE_T.equals(downloadFileFlag)) {
            return;
        }
        List<KitePosContractDTO> contracts = new ArrayList<KitePosContractDTO>();
        List<KiteOfferInst> offerInsts = querySceneInstService.listMainOfferInsts(context.getSceneInstId());
        offerInsts.forEach(offerInst -> {
            List<KiteOfferInstContract> offerInstContractRels = querySceneInstService
                .listOfferInstContracts(context.getCustId(), offerInst.getOfferInstId());
            if (Objects.isNull(offerInstContractRels)) {
                // ErrorConsts.BUSI_CHECK_ERROR.throwOut("合同/受理单信息不能为空");
                offerInstContractRels = new ArrayList<>();
            }
            offerInstContractRels = offerInstContractRels.stream()
                .filter(contract -> !KiteStringUtils.equals(contract.getActionType().name(), ActionType.X.name()))
                .collect(Collectors.toList());

            offerInstContractRels.forEach(offerInstContractRel -> {
                KitePosContractDTO contract = new KitePosContractDTO();
                contract.setInstanceId(offerInstContractRel.getInstId());
                contract.setBatchCode(offerInstContractRel.getContractBatchId());
                contract.setName(offerInstContractRel.getContractName());
                contract.setContractCode(offerInstContractRel.getContractNo());
                contract.setChangeType(ChangeTypeUtils.getChangeType(offerInstContractRel.getActionType().name()));
                if (ServiceOfferConsts.SERVICE_OFFER_TRANSFER.equals(kiteSceneInst.getServiceOfferId())) {
                    // 过户业务强制设置为修改
                    contract.setChangeType(ChangeTypeUtils.getChangeType(ActionType.M.name()));
                }
                List<KitePosAttachmentDTO> attachments = new ArrayList<KitePosAttachmentDTO>();
                KitePosAttachmentDTO attachement = new KitePosAttachmentDTO();
                // 合同/受理单-服务协议
                attachement.setCatagory("13");
                String attachementName = null;
                String attachementUrl = offerInstContractRel.getContractUrl();
                if (KiteStringUtils.contains(offerInstContractRel.getContractUrl(), "*_*")) {
                    attachementName = KiteStringUtils.substring(offerInstContractRel.getContractUrl(),
                        KiteStringUtils.lastIndexOf(offerInstContractRel.getContractUrl(), "*_*") + 3);
                    attachementUrl = KiteStringUtils.substring(attachementUrl, 0,
                        KiteStringUtils.lastIndexOf(offerInstContractRel.getContractUrl(), "*_*"));
                }
                else {
                    attachementName = KiteStringUtils.substring(offerInstContractRel.getContractUrl(),
                        KiteStringUtils.lastIndexOf(offerInstContractRel.getContractUrl(), "/") + 1);
                }
                attachement.setName(attachementName);
                attachement.setUrl(attachementUrl);
                attachments.add(attachement);
                contract.setAttachments(attachments);

                this.addInstanceIdAttr(context, offerInstContractRel.getInstId(), contract, contract.getChangeType());

                contracts.add(contract);
            });
        });

        context.getPosDto().setContract(contracts);
    }

}
